package com.lninl.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "users_uris")
public class UserUri extends IdEntity {
    private String title;//标题
    private String remark;//备注

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "uri_id")
    private Uri uri;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "users_uris_tags", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "user_id"), @JoinColumn(name = "uri_id", referencedColumnName = "uri_id")}, inverseJoinColumns = @JoinColumn(name = "tag_id"))
    private Set<Tag> tags;

    @JoinColumn(name = "category_id")
    @ManyToOne
    private Category category;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @JsonIgnore
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @JsonIgnore
    public Uri getUri() {
        return uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }

    @JsonIgnore
    public Set<Tag> getTags() {
        return tags;
    }

    public void setTags(Set<Tag> tags) {
        this.tags = tags;
    }

    @JsonIgnore
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @JsonProperty("username")
    public String getUsername() {
        return this.user.getUsername();
    }

    @JsonProperty("tags")
    public List<String> getTagsString() {

        List<String> tagsString = new ArrayList<>();
        for (Tag tag : tags) {
            tagsString.add(tag.getName());
        }
        return tagsString;
    }

    @JsonProperty("folder")
    public String getCategoryString() {
        if (this.category != null) {
            return this.category.getName();
        } else {
            return null;
        }
    }
}
